package com.speedchina.oauth2_permission.web.sys.api_v1;

import com.speedchina.framework.base.QueryModel;
import com.speedchina.framework.helper.Assert;
import com.speedchina.framework.model.R;
import com.speedchina.framework.validator.ValidatorHelper;
import com.speedchina.framework.validator.group.AddGroup;
import com.speedchina.framework.validator.group.UpdateGroup;
import com.speedchina.oauth2_permission.aspect.SysLog;
import com.speedchina.permission.base.constant.SysConstant;
import com.speedchina.permission.base.i18n.I18nService;
import com.speedchina.permission.domain.sys.po.DictItemPo;
import com.speedchina.permission.domain.sys.po.DictTypePo;
import com.speedchina.permission.service.sys.DictItemService;
import com.speedchina.permission.service.sys.DictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统表 - 字典项 接口
 * @author hank
 * @date 2021-04-08 13:31:41
 */
@RestController
@RequestMapping("/sys/dictItem")
@Api(description = "系统表 - 字典项 接口")
public class DictItemController {
    
    @Autowired
    private I18nService i18nService;

    @Autowired
    private DictItemService service;

    @Autowired
    private DictTypeService dictTypeService;

    @ApiOperation("分页查询")
    @GetMapping
    @SysLog("分页查询 - 字典项")
    //@PreAuthorize("hasAuthority('sys:dictItem:list')")
    public R query(Integer pageNo, Integer pageSize, DictItemPo params) {
        return R.page(service.queryPage(new QueryModel<>(pageNo, pageSize, params)));
    }

    @ApiOperation("根据类型编码查询全部")
    @SysLog("字典项 - 根据类型编码查询全部")
    @GetMapping("/queryAll")
    //@PreAuthorize("hasAuthority('sys:dictItem:list')")
    public R queryAll(DictItemPo params) {
        return R.data(service.queryList(params));
    }

    @ApiOperation("详情查询")
    @SysLog("字典项 - 详情查询")
    @GetMapping("/get")
    //@PreAuthorize("hasAuthority('sys:dictItem:info')")
    public R get(DictItemPo params) {
        return R.data(service.get(params));
    }

    @ApiOperation("新增")
    @ApiImplicitParams({
            // @ApiImplicitParam(name = "", value = "", dataType = "String", paramType = "path")
    })
    @PostMapping
    @SysLog("新增-系统表 - 字典项")
    @PreAuthorize("hasAuthority('sys:dictItem:add')")
    public R add(@RequestBody DictItemPo entity) {
        //查看类型存在才能新增
        DictTypePo dictTypePo = new DictTypePo();
        dictTypePo.setCode(entity.getDictCode());
        //数据不存在不让新增
        if (null == dictTypeService.get(dictTypePo)) {
            return R.error(i18nService.getMessage("DICT_TYPE_NOT_EXIST"));
        }
        //判断显示值和实际值不能重复 重复不让新增
        if (0 < service.countExist(entity)){
            return R.error(i18nService.getMessage("DICT__ITEM_REPEAT"));
        }
        ValidatorHelper.validateEntity(entity, AddGroup.class);
        return R.data(service.saveOrUpdate4AutoGenId(entity));
    }

    @ApiOperation("修改")
    @ApiImplicitParams({
            // @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", paramType = "path")
    })
    @PutMapping
    @SysLog("修改-系统表 - 字典项")
    @PreAuthorize("hasAuthority('sys:dictItem:update')")
    public R update(@RequestBody DictItemPo entity) {
        ValidatorHelper.validateEntity(entity, UpdateGroup.class);
        return R.data(service.saveOrUpdate4AutoGenId(entity));
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/{ids}")
    @SysLog("批量删除-系统表 - 字典项")
    @PreAuthorize("hasAuthority('sys:dictItem:delete')")
    public R delete(@PathVariable("ids") String strIds) {
        Assert.isNull(strIds, i18nService.getMessage("DELETE_CONDITION_ILLEGAL"));
        List<Serializable> ids = new ArrayList<>();
        for (String id : strIds.split(SysConstant.SPLIT_REGEX)) {
            if (StringUtils.isNotBlank(id)) {
                ids.add(id);
            }
        }
        Assert.isTrue(ids.isEmpty(), i18nService.getMessage("DELETE_CONDITION_ILLEGAL"));
        return R.data(service.deleteBatch(ids));
    }

    @GetMapping("/getTest")
    public R getTest(){

        String message = i18nService.getMessage("TEST_INFO");
        return R.data(message);
    }


}